<?php

namespace App\Http\Controllers\Wx;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Modules\Manager\Entities\Department;

class SendTemplateController extends WxCommonController
{
    // 公众号报销模板消息
    public function sendTemplateBx($data){
        $msg = $this->wechatApp->template_message->send([
            'touser' =>$data['openid'],
            'template_id' => 'Yp9vczuwziqeDURkI2pn1UhVmd2IyJDneo4nbcKKWSw',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> $data['page']??'pages/index/index',
            ],
            'data' => [
                'character_string1'=>$data['order_sn'],
                'const2'=>$this->getBxStatus($data['status']),
                'const3'=>$this->getBxType($data['type']),
                'amount4'=>$data['money'],
            ],
        ]);
        info(json_encode($msg));
    }
    public function sendTemplateSqc($data){
        $msg = $this->wechatApp->template_message->send([
            'touser' =>$data['openid'],
            'template_id' => 'grkcR4NUrIkxWra1jC_mDwfJasi4wf_ULYK1t4Uy_EE',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> $data['page']??'pages/index/index', // pages/admin/sqc/sqc，默认去送取车列表
            ],
            'data' => [
                'car_number8'=>$data['plate_number'], // 苏E02688X
                'thing12'=>$data['title'], // 车辆待送车
                'phrase5'=>$data['status'], // 待接单
            ],
        ]);
        info(json_encode($msg));
    }
    // 送车交接超时通知
    public function sendTemplateSccs($data){
        $msg = $this->wechatApp->template_message->send([
            'touser' =>$data['openid'],
            'template_id' => 'BSOuzQGQkGt9NVG_fY9VzdZaU8Ly3JegR3M7n1R6MUg',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> $data['page']??'pages/index/index', // pages/admin/sqc/sqc，默认去送取车列表
            ],
            'data' => [
                'thing10'=>$data['name'], // 客户姓名
                'thing13'=>$data['msg'], // 事件名称
                'thing8'=>$data['plate_number'], // 车辆
                'time9'=>$data['time'], // 超时时间
            ],
        ]);
        info(json_encode($msg));
    }
    // 月租订单对账通知
    public function sendTemplateYzdd($data){
        $msg = $this->wechatApp->template_message->send([
            'touser' =>$data['openid'],
            'template_id' => 'GjX59uYNHEA2w1G_Y6RSlK0omi7zfz80A-FpjzP2LQQ',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> $data['page']??'pages/admin/home/home',
            ],
            'data' => [
                'thing1'=>'月租订单对账通知', // 账单名称
                'character_string2'=>$data['order_sn'], // 账单编号
                'time3'=>now(), // 生成时间
                'thing5'=>$data['wx_name'], // 客户名称
                'time11'=>date('Y-m-d'), // 日期
            ],
        ]);
        info(json_encode($msg));
    }
    // 公众号新订单通知模板消息（通用-店长端通知）
    public function sendTemplateNewOrder($data){
        if ($data['wechat_openid']) {
            $msg = $this->wechatApp->template_message->send([
                'touser' =>$data['wechat_openid'],
                'template_id' => 'ZtUeGP70CNiZkT-6QoyothkmSmHUhVXcgfoj9i8DJho',
                'miniprogram' => [
                    'appid' => config('wechat.mini_program.default.app_id'),
                    'pagepath'=> $data['page']??'pages/index/index',
                ],
                'data' => [
                    'tradeDateTime'=>now(), // 提交时间 - 有效
                    'orderType'=>$data['orderType'], // 订单提示信息 - 有效
                    'customerInfo'=>$data['customerInfo'], // 客户信息 - 有效
                ],
            ]);
            info(json_encode($msg));
        }
    }
    public function sendTemplateByUser($data){
        $this->wechatApp->template_message->send([
            'touser' =>$data['openid'],
            'template_id' => 'Ql1lmAhkcEKKwNrcHTnNF-Gq8V2WiYL-tGCc4vnA9Jw',
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> 'pages/index/index',
            ],
            'data' => [
                'character_string2'=>$data['order_sn'],
                'thing3'=>$data['name'],
                'amount5'=>$data['money'],
                'time6'=>now(),
            ],
        ]);
    }
    public function sendTemplate($order){
        $admin = Admin::find(1);
        $this->wx->template_message->send([
            'touser' => $admin->openid,
            'template_id' => 'ZtUeGP70CNiZkT-6QoyothkmSmHUhVXcgfoj9i8DJho',

            'data' => [
                'first' => '您的小程序有新的订单请注意查收-_-!!',
                'tradeDateTime'=>now(),
                'orderType'=>'小程序客户下单',
                'customerInfo'=>$order->wx_name,
                'orderItemName'=>'兴趣车型',
                'orderItemData'=>$order->car_category
                //'remark'=>'智祥网络科技技术支持',
                //'key2' => 'VALUE2',
                //...
            ],
        ]);
    }
    public function sendTemplateWxAdmin($order){
        $info = $this->wx->template_message->send([
            'touser' => $order->openid,
            'template_id' => 'ZtUeGP70CNiZkT-6QoyothkmSmHUhVXcgfoj9i8DJho',
            'data' => [
                'first' => '您的小程序有新的订单请注意查收-_-!!',
                'tradeDateTime'=>now(),
                'orderType'=>'订单类型',
                'customerInfo'=>'客户信息',
                'orderItemName'=>'兴趣车型',
                'orderItemData'=>'经济型',
                'remark'=>'未付款',
            ],
        ]);
        info($info);
    }
    public function commonTemplate($request){
        $app = $this->wechatApp;
        return $res = $app->template_message->send([
            'touser' => $request['openid'],
            'template_id' => $request['template'],
            'data' => [
                'first' => $request['title'],
                'tradeDateTime'=>$request['time'], // 提交时间
                'orderType'=>$request['type'], // 订单类型
                'customerInfo'=>$request['username'],
                'orderItemName'=>$request['car_name'],
                'orderItemData'=>$request['content']
                //'remark'=>'智祥网络科技技术支持',
                //'key2' => 'VALUE2',
                //...
            ],
        ]);
    }
    // 获取报销状态
    public function getBxStatus($value){
        switch ($value){
            case 1:
                return '待审核';
            case 2:
                return '已拒绝';
            case 3:
                return '报销失败';
            case 4:
                return '报销成功';
            case 5:
                return '待支付审核';
        }
    }
    //  获取报销类型
    public function getBxType($value){
        switch ($value){
            case 1:
                return '车辆损耗';
            case 2:
                return '车辆支出';
            case 3:
                return '故障报修';
            case 4:
                return '保养';
        }
    }

//    获取部门
    public function getDepartment($id){
        Department::query()->where('id',$id)->value('name');
    }
    // 临近3小时订单
    public function preOrderOnTime($openid,$order){
        $app = $this->wechatApp;
        return $res = $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '预订单临近3小时',
                'tradeDateTime'=>$order->created_at, // 提交时间
                'orderType'=>$order->type, // 订单类型
                'customerInfo'=>$order->username,
                'orderItemName'=>$order->car_name,
                'orderItemData'=>$this->getDepartment($order->department).'预约提车时间:'.$order->pre_fetch_time.'请及时联系客户'
            ],
        ]);
    }
    // 回车临近2小时订单
    public function returnOrderOnTime($openid,$order){
        $app = $this->wechatApp;
        return $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '回车临近订单2小时',
                'tradeDateTime'=>$order->created_at, // 提交时间
                'orderType'=>$order->type, // 订单类型
                'customerInfo'=>$order->username,
                'orderItemName'=>$order->car_name,
                'orderItemData'=>'预约还车时间:'.$order->pre_return_time
            ],
        ]);
    }
    // 事故维修
    public function serviceAccident($openid,$order){
        $app = $this->wechatApp;
        return $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '回车临近订单2小时',
                'tradeDateTime'=>$order->created_at, // 提交时间
                'orderType'=>'事故维修', // 订单类型
                'customerInfo'=>'',
                'orderItemName'=>'',
                'orderItemData'=>'订单号:'.$order->id.',车牌号:'.$order->plate_number.'已于'.now().'完成维修'
            ],
        ]);
    }
    // 订单超时未完成
    public function orderOverTime($openid,$order){
        $app = $this->wechatApp;
        return $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '订单超时未操作',
                'tradeDateTime'=>$order->created_at, // 提交时间
                'orderType'=>$order->type, // 订单类型
                'customerInfo'=>'',
                'orderItemName'=>'',
                'orderItemData'=>'订单号:'.$order->order_sn.'已超时，请按实际租期操作续租'
            ],
        ]);
    }
}
